package com.example.demo.controller;


import com.example.demo.entity.User;
import com.example.demo.entity.rep.RefreshBody;
import com.example.demo.entity.rep.UserRep;
import com.example.demo.service.UserService;
import com.example.demo.utils.BusinessException;
import com.example.demo.utils.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody User user) {
        return Result.success(userService.register(user));
    }

    @PostMapping("/login")
    public Result<UserRep> login(@RequestBody User user) {
        return Result.success(userService.login(user.getAccount(), user.getPassword()));
    }

    @PostMapping("/refresh-token")
    public Result<UserRep> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        log.info("==> refreshToken: " + refreshToken);
        return Result.success(userService.refreshToken(refreshToken));
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody User user) {
        return Result.success(userService.updateUser(user));
    }

    //    获取用户列表
    @RequestMapping("/getAll")
    public Result<List<User>> getAll(String username, int pageNum, int pageSize) {
        return Result.success(userService.getAll(username, pageNum, pageSize));
    }

    //    根据username搜索
    @RequestMapping("/search")
    public Result<User> search(String username) {
        return Result.success(userService.getUserByUsername(username));
    }

}
